/**
 * lhj
 */

import JsSha from 'jssha'
import { Md5 } from 'ts-md5'

export default class MCryptoTs{

    public static sha1(data: string): string | null {
        return MCryptoTs.sha(data, Algo.SHA1)
    }

    public static sha256(data: string): string | null {
        return MCryptoTs.sha(data, Algo.SHA256)
    }

    public static sha512(data: string): string | null {
        return MCryptoTs.sha(data, Algo.SHA512)
    }

    public static sha(data: string, algo?: Algo, inputFormat = InputFormat.TEXT): string | null {
        if (!data || !algo) return null
        const shaObj = new JsSha(algo, inputFormat, {
            encoding: 'UTF8'
        })
        shaObj.update(data)
        return shaObj.getHash('HEX')
    }

    public static md5(data: string): string | null {
        if (!data) return null
        return Md5.hashStr(data).toString()
    }
}

enum Algo{
    SHA1='SHA-1',
    SHA224='SHA-224',
    SHA256='SHA-256',
    SHA512='SHA-512',
}

enum InputFormat{
    TEXT='TEXT',
}